import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;

import org.openqa.selenium.support.ui.WebDriverWait;


import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {

    public static void main(String[] args) throws InterruptedException, IOException {


        //test01();
        //test02();
       // test03();
        //test04();
       //test05();
       //test06();
        //test07();
        //test08();
        //test09();
       // test10();
        test11();

    }

    private static void test11() throws InterruptedException {
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver=new ChromeDriver(options);

        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(1000);

        Set<String> handles= webDriver.getWindowHandles();//获取所有的窗口句柄

        String target_handle="";
        for(String handle:handles){
            target_handle=handle;
        }
        //遍历完毕到最后一个窗口
        System.out.println(target_handle);
        webDriver.switchTo().window(target_handle);//切换到最后一个窗口
        sleep(3000);
        //在切换到的窗口点击音乐
        webDriver.findElement(By.cssSelector("#header-link-wrapper > li:nth-child(5) > a")).click();
        sleep(3000);
        webDriver.quit();



    }
    //窗口切换





    //关闭操作
    private static void test10() throws InterruptedException {
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver=new ChromeDriver(options);

        webDriver.get("https://www.baidu.com");
        sleep(1000);
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(1000);
        webDriver.close();//关闭的是已经打开的页面
        sleep(1000);
        webDriver.quit();//关闭的是整个浏览器

        //quit()直接清除cookie信息
        //close()不清楚cookie信息

    }







  //截图
    private static void test09() throws IOException {
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver=new ChromeDriver(options);

        webDriver.get("https://www.baidu.com");
        //打开百度首页，将百度首页截图放到 ：C:\Users\南方有乔木\Desktop\图片\jietu.png 目录下
        //进行文件IO操作需要将引入依赖 common-io
        File src_file=((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(src_file,new File("C:\\Users\\南方有乔木\\Desktop\\图片\\jietu.png"));

    }

    private static void test08() throws InterruptedException {
        //打开百度以后输入软件测试，点击搜索，搜索后滑动滚动条到底部
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver=new ChromeDriver(options);

        webDriver.get("https://www.baidu.com");

        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);//程序休眠3秒
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000;");//向下滑动滚动条

    }

    private static void test07() throws InterruptedException {
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver=new ChromeDriver(options);
        webDriver.get("http://localhost:63342/selenium02/src/main/page/test02.html?_ijt=kbl9v8fml4iscsmk2cr47m4a8s&_ij_reload=RELOAD_ON_SAVE");

        sleep(3000);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//隐式等待3秒,等待页面所有元素

        WebDriverWait wait = new WebDriverWait(webDriver, Duration.ofSeconds(5));//显示等待5秒
       wait.until(ExpectedConditions.textToBe(By.cssSelector("h3"),"frame"));//显示等待：只要满足条件即可


    }

    //上传文件
    private static void test06() {
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver=new ChromeDriver(options);
        webDriver.get("http://localhost:63342/selenium02/src/main/page/test05.html?_ijt=jgon3cqt4s6j9f11q49upkln2b&_ij_reload=RELOAD_ON_SAVE");
        webDriver.findElement(By.cssSelector("body > input[type=file]")).sendKeys("C:\\Users\\南方有乔木\\Desktop\\avatar01.jpeg");


    }


    //操作alert弹窗
    private static void test05() throws InterruptedException {
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver=new ChromeDriver(options);
        webDriver.get("http://localhost:63342/selenium02/src/main/page/test04.html?_ijt=tq3a0260r0k3p7nu1g2l1720vm");

        webDriver.findElement(By.cssSelector("body > button")).click();//定位弹窗并点击
        sleep(3000);
       // webDriver.switchTo().alert().accept();//锁定弹窗并确定
      //  webDriver.switchTo().alert().dismiss();//锁定弹窗并取消

        webDriver.switchTo().alert().sendKeys("南方有乔木，你好");//alert输入并确定
        webDriver.switchTo().alert().accept();

    }

    //操作下拉选项
    private static void test04() {
       ChromeOptions options=new ChromeOptions();
       options.addArguments("--remote-allow-origins=*");
       WebDriver webDriver=new ChromeDriver(options);

       webDriver.get("http://localhost:63342/selenium02/src/main/page/test03.html?_ijt=ugcpt8nkhptred5grc4l3b5od5");
       //进入下拉框

        Select select=new Select(webDriver.findElement(By.cssSelector("#ShippingMethod")));
       // 方法1： select.selectByValue("9.25");
        select.selectByIndex(2);
    }

    //多层级框架iframe页面操作
    private static void test03() {
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver=new ChromeDriver(options);
        webDriver.get("http://localhost:63342/selenium02/src/main/page/test02.html?_ijt=t8ntl0hi7rksg2m7tepffirgsg");
      //在iframe标签下先切换层级再操作
        webDriver.switchTo().frame("f1");
        webDriver.findElement(By.cssSelector("body > div > div > a")).click();


        //不在iframe标签下可以直接操作
     //  String text= webDriver.findElement(By.cssSelector("body > div > div > h3")).getText();
      //  System.out.println(text);
    }

    private static void test02(){
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver=new ChromeDriver(options);
//选中所需要选中的input类型
        webDriver.get("http://localhost:63342/selenium02/src/main/page/page.html?_ijt=r910oaoderel3eocarrojhin9q");
         List<WebElement> webElementList=webDriver.findElements(By.cssSelector("input"));

         for(int i=0;i<webElementList.size();i++){
             if(webElementList.get(i).getAttribute("type").equals("radio")){

             }else{
                 webElementList.get(i).click();
             }

         }
    }


    private static void test01(){
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");//允许跨域请求
        WebDriver webDriver=new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");//打开百度页面
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");//定位搜索框输入软件测试
        webDriver.findElement(By.cssSelector("#su")).click();//点击百度一下搜索
        //搜索后等待三秒
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        Actions actions=new Actions(webDriver);//进入鼠标事件
        WebElement target=webDriver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic"));
        // actions.moveToElement(target).perform();//执行将鼠标移动到图片事件

        actions.contextClick(target).perform();//执行右击事件
    }
}
